<?php

namespace App\Models;

use Cache;

class Setting extends BaseModel
{
    protected $table = "setting";

    const KEY = 'setting_cache_all1';

    public function __construct()
    {
        parent::__construct();
    }

    public function getList($where = [])
    {
        return $this->multiWhere($where)->multiOrder(['id' => 'desc'])->get();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
        $res = $this->insertGetId($data);
        if ($res) {
            $this->gennerateCache();
        }
        return $res;
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        $res = $this->multiWhere($where)->update($data);
        if ($res) {
            $this->gennerateCache();
        }
        return $res;
    }

    public function getSettingList()
    {
        $rows = $this->getList([], '*', ['id' => 'desc'], 10);
        return $rows;
    }

    public function gennerateCache()
    {
        Cache::forget(self::KEY);
        $this->getAllCache();
    }

    public function getAllCache()
    {
        $data = Cache::get(self::KEY);
        if (!$data) {
            $data = $this->getList();
            if ($data) {
                $data = $data->toArray();
                $arr = '';
                foreach ($data as $v) {
                    $arr[$v['type']][$v['name']] = $v['value'];
                    if (strstr($v['value'], ',')) {
                        $arr[$v['type']][$v['name']] = explode(',', $v['value']);
                    }
                }
                Cache::forever(self::KEY, $arr);
                $data = $arr;
            }
        }
        return $data;
    }

    public function getSetting($type)
    {
        $res = $this->getAllCache();
        return $res[$type];
    }

    public function editSetting($request)
    {
        $where = '';
        switch ($request['type']) {
            case 1:
                $where['type'] = 'sms';
                $data['account_sid'] = trim($request['account_sid']);
                $data['account_token'] = trim($request['account_token']);
                $data['app_id'] = trim($request['app_id']);
                $data['server_ip'] = trim($request['server_ip']);
                $data['server_port'] = trim($request['server_port']);
                $data['soft_version'] = trim($request['soft_version']);
                break;
            case 2:
                $where['type'] = 'coin';
                $data['btc'] = intval($request['btc']);
                $data['eth'] = intval($request['eth']);
                break;
            case 3:
                $where['type'] = 'system';
                $data['email'] = htmlspecialchars($request['email']);
                $data['icontime'] = htmlspecialchars($request['ico']);
                break;
        }
        foreach ($data as $k => $v) {
            if ($v) {
                $where['name'] = $k;
                $res = $this->up($where, ['value'=>$v]);
                if (!$res) {
                    return '保存失败';
                }
            }
        }
        return '保存成功';
    }

}
